﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Pong_Project.Views
{
    class About : View
    {
        // Font list
        private SpriteFont headFont, longFont, backFont;

        // Text display padding
        private Vector2 padding;

        public About()
            : base()
        {
            // Set default padding
            padding = new Vector2(50, 20);
        }

        public override void LoadContent(ContentManager content)
        {
            headFont = content.Load<SpriteFont>("Head");
            longFont = content.Load<SpriteFont>("Long");
            backFont = content.Load<SpriteFont>("Back");
        }

        public override void Update(GameTime gameTime)
        {
            if (Controls.MuteSound)
            {
                MediaPlayer.IsMuted = !MediaPlayer.IsMuted;
            }

            if (Controls.Enter)
            {
                // Go back to menu
                ViewManager.State = GameState.Menu;
            }
        }

        public override void Draw(SpriteBatch spriteBatch)
        {
            String longContent = String.Empty;
            String backContent = "Back to menu";
            String headContent = "About";

            longContent += "This game is made by Group X1 in the Software Architecture-course at NTNU. ";
            longContent += "Pong Ultimate is based on the classic game of Pong by Atari, but we added some extra hot stuff like power ups. ";
            longContent += "This is a game in constant progress, meant for personal use only. If you enjoyed this game or got ideas how to make it better, please contact us! ";

            // Get available viewport width
            Int32 availableWidth = (Int32)Math.Round(spriteBatch.GraphicsDevice.Viewport.Width - 2 * padding.X);

            // Set minimal width
            availableWidth = Math.Max(100, availableWidth);

            // Modify the text to fit into viewport
            longContent = StringHelper.wrap(longFont, longContent, availableWidth);

            // Display text positions
            Vector2 headPosition = new Vector2(spriteBatch.GraphicsDevice.Viewport.Width / 2, 0);
            Vector2 longPosition = new Vector2(padding.X, padding.Y);
            Vector2 backPosition = new Vector2(spriteBatch.GraphicsDevice.Viewport.Width / 2, spriteBatch.GraphicsDevice.Viewport.Height);

            // Find right side of the left string
            Vector2 headOrigin = headFont.MeasureString(headContent);
            Vector2 backOrigin = backFont.MeasureString(backContent);

            // Modify position according to size
            headPosition = new Vector2(headPosition.X - headOrigin.X / 2, headPosition.Y + padding.Y);
            longPosition = new Vector2(longPosition.X, longPosition.Y + headOrigin.Y + padding.Y);
            backPosition = new Vector2(backPosition.X - backOrigin.X / 2, backPosition.Y - padding.Y - backOrigin.Y);

            // Draw current score to the ground
            spriteBatch.DrawString(headFont, headContent, headPosition, Color.White, 0.0f, Vector2.Zero, 1.0f, SpriteEffects.None, 0.0f);
            spriteBatch.DrawString(longFont, longContent, longPosition, Color.White, 0.0f, Vector2.Zero, 1.0f, SpriteEffects.None, 0.0f);
            spriteBatch.DrawString(backFont, backContent, backPosition, Color.Red, 0.0f, Vector2.Zero, 1.0f, SpriteEffects.None, 0.0f);
        }
    }
}
